package cn.heroes.weixin.services;

import java.util.Date;

import cn.heroes.weixin.pojo.Message;
import cn.heroes.weixin.type.EventType;
import cn.heroes.weixin.type.MessageType;

/**
 * 微信公众平台功能调度类 TODO 写成接口
 * 
 * @author cq
 * 
 */
public class TestService {

	public static Message whenGetEventMsg(Message msg) {
		String event = msg.event;
		Message res = null;
		if (EventType.SUBSCRIBE.equals(event)) {
			// 订阅(关注)
			res = createTextMessage(msg);
			// Axiom号全体人员
			res.content = "感谢您的关注！\n我们将定期推送一些电影讯息。";
		} else if (EventType.UNSUBCRIBE.equals(event)) {
			// 取消订阅(关注)
			res = createTextMessage(msg);
			res.content = "期待下一次关注！";
		}
		return res;
	}

	/**
	 * 根据用户发来的信息创建除内容外的文本信息
	 * 
	 * @param fromMsg
	 * @return
	 */
	private static Message createTextMessage(Message fromMsg) {
		Message res = new Message();
		res.fromUserName = fromMsg.toUserName;
		res.toUserName = fromMsg.fromUserName;
		res.msgType = MessageType.TEXT;
		res.createTime = new Date().getTime();
		return res;
	}

	/**
	 * 接收用户普通消息时
	 * 
	 * @param msg
	 * @return
	 */
	public static Message whenGetTextMsg(Message msg) {
		// illegal: username;
		// legal: createtime; msgId; header; \n
		Message res = createTextMessage(msg);
		res.content = "你好!\nWe got your message \"" + msg.content + "\"";
		return res;
	}
}
